package com.etcbase.dcs.utils;

import android.app.Fragment;
import android.app.LocalActivityManager;
import android.os.Bundle;
import android.util.Log;

public class LocalActivityManagerFragment extends Fragment {

	private static final String TAG = LocalActivityManagerFragment.class
			.getSimpleName();
	private static final String KEY_STATE_BUNDLE = "localActivityManagerState";

	private LocalActivityManager localActivityManager;

	protected LocalActivityManager getLocalActivityManager() {
		return localActivityManager;
	}

	@Override
	public void onCreate(Bundle savedInstanceBundle) {
		super.onCreate(savedInstanceBundle);
		Log.d(TAG, "onCreate(): " + getClass().getSimpleName());

		Bundle state = null;
		if (savedInstanceBundle != null) {
			state = savedInstanceBundle.getBundle(KEY_STATE_BUNDLE);
		}

		localActivityManager = new LocalActivityManager(getActivity(), true);
		localActivityManager.dispatchCreate(state);
	}

	@Override
	public void onSaveInstanceState(Bundle outStateBundle) {
		super.onSaveInstanceState(outStateBundle);
		outStateBundle.putBundle(KEY_STATE_BUNDLE,
				localActivityManager.saveInstanceState());
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume(): " + getClass().getSimpleName());
		localActivityManager.dispatchResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause(): " + getClass().getSimpleName());
		localActivityManager.dispatchPause(getActivity().isFinishing());
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop(): " + getClass().getSimpleName());
		localActivityManager.dispatchStop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy(): " + getClass().getSimpleName());
		localActivityManager.dispatchDestroy(getActivity().isFinishing());
	}
}
